package com.aaa.base.modules.visitor.aliyun;

import com.alibaba.fastjson.JSON;
import com.aliyun.ocr.Client;
import com.aliyun.ocr.models.Config;
import com.aliyun.ocr.models.RecognizeLicensePlateAdvanceRequest;
import com.aliyun.ocr.models.RecognizeLicensePlateResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aaa.base.modules.visitor.utils.PicUtils;

import java.io.InputStream;
import java.util.Map;

/**
 * 选择阿里云达摩院视觉智能开放平台java本地上传，否则要开通oss
 *
 * 前端不用穿原图，就传压缩的好了
 */
public class AliYunDemo3 {

    //阿里云appId
    static String accessKeyId = "";
    static String accessKeySecret = "";
    static Client ocrClient;
    static RuntimeOptions runtime;

    public static void main(String[] args) {
        init();
        //身份证正面
        // String filePath = "http://dev.aaa.cn/fastdfs/img/M00/01/01/wKi8Gl9oRkSAVxnhAAJmGac6sTM610.jpg";
        //身份证反面
        // String filePath = "http://dev.aaa.cn/fastdfs/img/M00/01/01/wKi8Gl9oRkuARx0aAAL_WZXZilA420.jpg";

        //车牌
        String filePath ="http://dev.aaa.cn/fastdfs/img/M00/01/01/wKi8Gl9oSSaAZroSAAB59_YeE2A295.jpg";

        //银行卡
        // String filePath ="http://dev.aaa.cn/fastdfs/img/M00/01/0B/wKi8Gl9ymuuAf0gfAA6jE_AqzdA116.jpg";


        //只能本地，不能网络URL
        // String filePath = "C:\\Users\\aaa\\Desktop\\身份证识别\\1122714795.jpg";
        // Map<String, String> map = recognizeIdCard(filePath, "face");
        //阿里云身份证识别，需要用户传 正面，还是 背面，否则报错com.aliyun.tea.TeaException: Side is mandatory for this action.智能个鬼。反观腾讯云并不需要传正反面
        Map<String, String> map = recognizeIdCard(filePath, "back");


        System.out.println(map);
    }

    public static void init() {
        try {
            Config config = new Config();
            config.type = "access_key";
            config.regionId = "cn-shanghai";
            config.accessKeyId = accessKeyId;
            config.accessKeySecret = accessKeySecret;
            config.endpoint = "ocr.cn-shanghai.aliyuncs.com";

            ocrClient = new Client(config);
            runtime = new RuntimeOptions();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<String, String> recognizeIdCard(String filePath, String side) {
        //出现advance表示使用本地文件，没有则表示使用公网可以访问的URL
        try {
            //身份证request
            // RecognizeIdentityCardAdvanceRequest request = new RecognizeIdentityCardAdvanceRequest();

            //银行卡request
            // RecognizeBankCardAdvanceRequest request= new RecognizeBankCardAdvanceRequest();

            //车牌request
            RecognizeLicensePlateAdvanceRequest request=new RecognizeLicensePlateAdvanceRequest();

            //读取输入流即可(本地)
            // request.imageURLObject = Files.newInputStream(Paths.get(filePath));
            // URL
            InputStream inputStream= PicUtils.getInputStreamOfImageFromNetByUrl(filePath);
            request.imageURLObject = inputStream;

            // 身份证正反面
            // request.side = side;
            //身份证
            // RecognizeIdentityCardResponse response = ocrClient.recognizeIdentityCardAdvance(request, runtime);


            //银行卡
            // RecognizeBankCardResponse response = ocrClient.recognizeBankCardAdvance(request, runtime);


            //车牌
            RecognizeLicensePlateResponse response= ocrClient.recognizeLicensePlateAdvance(request, runtime);


            /**
             * com.aliyun.tea.TeaException: Specified file url parameter http://dev.aaa.cn/fastdfs/img/M00/01/01/wKi8Gl9oRkSAVxnhAAJmGac6sTM610.jpg must be a valid oss url
             * 阿里云支持URL的方法有坑。我还是本地吧。
             */
            // RecognizeBankCardRequest req = new RecognizeBankCardRequest();
            // req.imageURL=filePath;
            // RecognizeBankCardResponse rep = ocrClient.recognizeBankCard(req, runtime);
            // System.out.println("身份证识别="+JSON.toJSONString(rep));

            System.out.println("识别结果response.data--"+JSON.toJSONString(response.data));
            return null;

            //身份证
            // if ("face".equals(side)) {
            //     //泛型传递
            //     return JSON.parseObject(JSON.toJSONString(response.data.frontResult), new TypeReference<Map<String, String>>() {
            //     });
            // } else {
            //     return JSON.parseObject(JSON.toJSONString(response.data.backResult), new TypeReference<Map<String, String>>() {
            //     });
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
